package com.lowcodeai.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lowcodeai.auth.entity.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户信息Mapper
 * 
 * @author LowCodeAI
 * @since 1.0.0
 */
@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {

    /**
     * 根据用户名查询用户信息
     */
    @Select("SELECT * FROM user_info WHERE username = #{username} AND deleted = 0")
    UserInfo selectByUsername(@Param("username") String username);

    /**
     * 根据用户ID查询角色编码列表
     */
    @Select("SELECT r.role_code FROM role_info r " +
            "INNER JOIN user_role ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND r.deleted = 0 AND r.status = 1")
    List<String> selectRoleCodesByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID查询权限编码列表
     */
    @Select("SELECT DISTINCT p.permission_code FROM permission_info p " +
            "INNER JOIN role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.deleted = 0 AND p.status = 1")
    List<String> selectPermissionCodesByUserId(@Param("userId") Long userId);
} 